![]() |
PATH![]() |
Sometimes you may want to determine or set a file type or creator for a file that already exists. For example, if the user requests that a file be opened, the application might check the file type to make sure it can handle that type of file. You can read the file type and creator using the getFileType and getFileCreator methods respectively. To set the file type and creator, you can use either the setFileType and setFileCreator methods or the setFileTypeAndCreator method. The code fragment in Listing 1-2 shows an example of reading and setting the file type and creator.
Listing 1-2 Reading and setting the file type and creator
try {
MRJOSType curType = MRJFileUtils.getFileType(f);
MRJOSType curCreator = MRJFileUtils.getFileCreator(f);
// make sure they're what we expect
if (! (curType.equals(new MRJOSType ("TEXT") &&
curCreator.equals(new MRJOSType ("ttxt"))))
throw new IOException("Unexpected file type or creator");
} catch (IOException e) {
fail("Couldn't get file type or creator", e);
return;
}
try {
MRJFileUtils.setFileTypeAndCreator(f, new MRJOSType("TEXT"),
new MRJOSType("CWIE"));
} catch (Exception e) {
fail("Can't set file type and creator", e);
return;
}
This example checks to see if the file in question is a SimpleText text file (file type 'TEXT' , creator 'ttxt' ). If so, it then changes the file type and creator to be that of a CodeWarrior text file. Note that since the file type is not changed, you could have called the setFileCreator method to just set the creator to 'CWIE' . After changing the creator, double-clicking on the file causes the file to be opened using CodeWarrior rather than SimpleText.